package com.bm.server.serverImpl;

import com.bm.dao.*;
import com.bm.pojo.addr.PlaceOrigin;
import com.bm.pojo.disease.Disease;
import com.bm.pojo.sort.GrapeSort;
import com.bm.pojo.wine.Wine;
import com.bm.server.Neo4jOperationServer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 * User: sx
 * Date: 2022/4/27
 * Time: 19:29
 * Description: java
 */
@Service
public class Neo4jOperationServerImpl implements Neo4jOperationServer {

    @Resource
    private Neo4jIndexDao neo4jIndexDao;

    @Resource
    private Neo4jGrapeRepository neo4jGrapeRepository;

    @Resource
    private Neo4jDiseaseRepository neo4jDiseaseRepository;

    @Resource
    private Neo4jPlaceOriginRepository neo4jPlaceOriginRepository;

    @Resource
    private Neo4jWineRepository neo4jWineRepository;

    /**
     *
     * @param id 需要创建关系的节点id
     * @param name 创建节点的名
     * @param details 创建节点的详细情况
     * @param label 新加节点标签
     * @param sort 与创建节点的关系
     * @return
     */
    @Override
    public String addNode(Integer id, String name, String details, String label, String sort) {

        //查询相似度
        Boolean similarity = neo4jIndexDao.getSimilarity(name);
        if(similarity){
            return "节点已存在";
        }
        Long getId=null;
        if("grapeTu".equals(label)){
            GrapeSort grapeSort = new GrapeSort();
            grapeSort.setDetails(details);
            grapeSort.setLabel(label);
            grapeSort.setGrapeName(name);
            GrapeSort save = neo4jGrapeRepository.save(grapeSort);
            getId = save.getId();
        }else if("disease".equals(label)){
            Disease disease = new Disease();
            disease.setDetails(details);
            disease.setLabel(label);
            disease.setName(name);
            Disease save = neo4jDiseaseRepository.save(disease);
            getId=save.getId();
        }else if("placeOrigin".equals(label)){
            PlaceOrigin placeOrigin = new PlaceOrigin();
            placeOrigin.setDetails(details);
            placeOrigin.setLabel(label);
            placeOrigin.setName(name);
            PlaceOrigin save = neo4jPlaceOriginRepository.save(placeOrigin);
            getId = save.getId();
        }else {
            Wine wine = new Wine();
            wine.setDetails(details);
            wine.setLabel(label);
            wine.setName(name);
            Wine save = neo4jWineRepository.save(wine);
            getId = save.getId();
        }
        long pid = getId;
        int pid1 = (int) pid;
        boolean b = neo4jIndexDao.addLink(id, pid1, sort);
        if(b){
            return "保存成功";
        }
        return "保存失败";
    }
}
